package com.whu;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;

@WebServlet("/myServlet")
public class myServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        HttpSession session = req.getSession(true);
        String tableName = req.getParameter("tableName");
        session.setAttribute("tableNam", tableName);
        String sql = "";
        String choose = req.getParameter("op");
        if(null == tableName && null != Table.getTableName()){
            tableName = Table.getTableName();
        }
        System.out.println("String tableName为："+tableName);
        Table table = null;
        String value;
        HashMap<String,Object> map;
        map = new HashMap<>();
        if(session.getAttribute("table")!=null){
            table = (Table) session.getAttribute("table");
            for(int i = 0;i<table.getColumn();i++){
                String name = "info"+i;
                String data = req.getParameter(name);
                if(null==data) data="";
                map.put(table.getElements()[i],data);
            }
        }
        switch (choose) {
            case "submit":
                sql = req.getParameter("sqlFillIn");
                System.out.println("submit的sql为："+sql);
                if(DBTest.funcWhenSubmit(sql)){
                    //funcWhenSubmit(sql)为true，说明了sql语句不是select语句，需要修改
                    sql = "select * from " + Table.getTableName() + " order by "+table.getElements()[table.getColumn()-1];
                }
                break;
            case "select":
                sql = "select * from " + tableName + " order by "+table.getElements()[table.getColumn()-1];
                break;
            case "add":
                if(table!=null){
                    value = "'" + map.get(table.getElements()[0]);
                    for(int i = 1;i<table.getColumn();i++){
                        value += "', '";
                        value += map.get(table.getElements()[i]);
                    }
                    value += "'";
                    sql = "insert into " + tableName + " values(" + value + ")";
                    System.out.println(sql);
                    DBTest.executeTable(sql);
                }
                sql = "select * from " + tableName + " order by "+table.getElements()[table.getColumn()-1];
                break;
            case "delete":
                if(table!=null){
                    value = table.getElements()[table.getColumn()-1] + " = "
                            + "'" + map.get(table.getElements()[table.getColumn()-1]) + "'";
                    sql = "delete from " + tableName + " where " + value;
                    System.out.println(sql);
                    DBTest.executeTable(sql);
                }
                sql = "select * from " + tableName + " order by "+table.getElements()[table.getColumn()-1];
                break;
            case "update":
                if(table!=null){
                    value = table.getElements()[0] + " = "
                            + "'" + map.get(table.getElements()[0]) + "'";
                    for(int i = 1;i<table.getColumn();i++){
                        String temp = " , "  + table.getElements()[i] + " = "
                                + "'" + map.get(table.getElements()[i]) + "'";
                        value += temp;
                    }
                    //where后面的内容
                    String condition = table.getElements()[table.getColumn()-1] + " = "
                            + "'" + map.get(table.getElements()[table.getColumn()-1]) + "'";
                    sql = "update " + tableName + " set " + value + " where " + condition;
                    DBTest.executeTable(sql);
                }
                sql = "select * from " + tableName + " order by "+table.getElements()[table.getColumn()-1];
                break;
            default:break;
        }

        session.setAttribute("sql", sql);

        RequestDispatcher rs = req.getRequestDispatcher("main.jsp");
        rs.forward(req, resp);
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);
    }

}
